package oneD.t3dviewer;
/* Cylinder.java  Generate a cylinder for Java3D

   History:

   04 Oct 2005  Decrease heap use.  Label some constants.
   30 Sep 2005  Created from Sun's Cylinder.java except make it
                stretch along x-axis instead of y-axis, don't
                bother with x and z offsets, and optimize a bit.
 */

import javax.media.j3d.*;
import javax.vecmath.*;

public class Cylinder {
   
   public final static int VERTEXES_PER_QUAD = 4;   
   public final static int COORDINATES_PER_VERTEX = 3;   
   public final static int COORDINATES_PER_QUAD =
     VERTEXES_PER_QUAD*
     COORDINATES_PER_VERTEX;   
   Shape3D shape;

   public Cylinder(double radius, double length, int quality, Appearance a) {

      if (quality < 3) quality = 3;
      double inc = 2.0*Math.PI/(double)quality;
      QuadArray quad =
         new QuadArray(quality*VERTEXES_PER_QUAD,
		       QuadArray.COORDINATES | QuadArray.NORMALS );
      float data[] = new float[COORDINATES_PER_QUAD];

      float ny2 = 0.0f;
      float nz2 = 1.0f;
      float y2 = 0.0f;
      float z2 = (float)radius;
      for (int i = 0; i < quality; i++) {
	 float ny1 = ny2;
	 float nz1 = nz2;
	 ny2 = (float)Math.sin((i + 1)*inc);
	 nz2 = (float)Math.cos((i + 1)*inc);
	 float y1 = y2;
	 float z1 = z2;
	 y2 = (float)radius * ny2;
	 z2 = (float)radius * nz2;

	 data[0]  = 0.0f;
	 data[1]  = y1;
	 data[2]  = z1;
	 data[3]  = (float)length;
	 data[4]  = y1;
	 data[5]  = z1;
	 data[6]  = (float)length;
	 data[7]  = y2;
	 data[8]  = z2;
	 data[9]  = 0.0f;
	 data[10] = y2;
	 data[11] = z2;
         quad.setCoordinates(i*VERTEXES_PER_QUAD, data);

	 data[0] = 0.0f;
	 data[1] = ny1;
	 data[2] = nz1;
	 data[3] = 0.0f;
	 data[4] = ny1;
	 data[5] = nz1;
	 data[6] = 0.0f;
	 data[7] = ny2;
	 data[8] = nz2;
	 data[9] = 0.0f;
	 data[10] = ny2;
	 data[11] = nz2;
         quad.setNormals(i*VERTEXES_PER_QUAD, data);
      }
      
      shape = new Shape3D(quad, a);
   }

   Shape3D getShape(){
      return shape;
   }
}



